package cn.pping.mq.consumer;

import cn.pping.domain.MediaFile;
import cn.pping.service.IMediaFileService;
import com.alibaba.fastjson.JSONObject;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
@RocketMQMessageListener(
        consumerGroup = "service-media-consumer",//消费者组名
        topic = "topic-media",//topic
        selectorExpression = "tag-media",//tag
        messageModel = MessageModel.CLUSTERING //集群
)
public class MediaConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private IMediaFileService mediaFileService;

    @Override
    public void onMessage(MessageExt messageExt) {
        if (messageExt.getBody() == null) {
            return;
        }
        String s = new String(messageExt.getBody(), StandardCharsets.UTF_8);
        //消费业务
        MediaFile mediaFile = JSONObject.parseObject(s, MediaFile.class);
        mediaFileService.handleFile2m3u8(mediaFile);


    }
}
